import { RefObject, useEffect } from 'react';
import { isReturnStatement } from 'typescript';

function useClickOutside(ref:RefObject<HTMLElement>,handler:Function){
  useEffect(()=>{
    //看点击的节点是否包含
    const listener=(event:MouseEvent)=>{
      if(!ref.current||ref.current.contains(event.target as HTMLElement)){
        return
      }
      handler(event)
    }
    document.addEventListener('click',listener)
    return ()=>{
      document.removeEventListener('click',listener)
    }
  },[ref,handler])
}

export default useClickOutside